import 'package:aihu_getx_app/app/modules/commons/model/dict_data_model.dart';
import 'package:aihu_getx_app/app/modules/commons/model/image_obj.dart';
import 'package:aihu_getx_app/app/modules/d/d_my_user_job_card/controllers/d_my_user_job_card_controller.dart';
import 'package:aihu_getx_app/utils/constants.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:json_annotation/json_annotation.dart';

part 'user_job_card_resp_vo.g.dart';

@JsonSerializable()
class UserJobCardRespVO {
  int? id, userId, deptId;
  String? appId, cardNo, qrCode, createTime;
  DictDataModel? userIdDictData, appIdDictData;
  List<ImageObj>? qrBg;
  final qrBgIndex = 0.obs;
  final qrBgImage = const DecorationImage(
    image: AssetImage(
      Constants.defaultQrBg,
    ),
    fit: BoxFit.fill,
  ).obs;

  UserJobCardRespVO({
    this.id,
    this.userId,
    this.deptId,
    this.appId,
    this.cardNo,
    this.qrCode,
    this.createTime,
    this.userIdDictData,
    this.appIdDictData,
    this.qrBg,
  });

  factory UserJobCardRespVO.fromJson(Map<String, dynamic> json) =>
      _$UserJobCardRespVOFromJson(json);

  void changeQrBg() {
    if (!GetUtils.isNullOrBlank(qrBg)!) {
      qrBgIndex.value = (qrBgIndex.value + 1) % qrBg!.length;
      qrBgImage.value = _getCurrentBgImage();
      Get.find<DMyUserJobCardController>().update();
    }
  }

  DecorationImage _getCurrentBgImage() {
    if (GetUtils.isNullOrBlank(qrBg)!) {
      return const DecorationImage(
        image: AssetImage(
          Constants.defaultQrBg,
        ),
        fit: BoxFit.fill,
      );
    }
    return DecorationImage(
      image: NetworkImage(qrBg![qrBgIndex.value].url!),
      fit: BoxFit.fill,
    );
  }

  Map<String, dynamic> toJson() => _$UserJobCardRespVOToJson(this);
}
